package com.wjj.happytime.activity.listen_activity;

import static com.wjj.happytime.service.PlayService.ACTION_PLAY;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.wjj.happytime.R;
import com.wjj.happytime.service.PlayService;

public class PlayActivity extends AppCompatActivity implements OnClickListener {

  public ImageView ivPlayPic;
  public ImageView ivControl;
  public String playUrl;
  public Button btnPlay;
  public Button btnStop;
  public String radioName;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play);
    initView();
    initData();
    initEvent();
  }

  private void initEvent() {
    ivPlayPic.setOnClickListener(this);
    btnPlay.setOnClickListener(this);
    btnStop.setOnClickListener(this);
    playRadio();
  }



  private void initData() {
    playUrl = getIntent().getStringExtra("playUrl");
    radioName = getIntent().getStringExtra("radioName");
    Toast.makeText(this,playUrl, Toast.LENGTH_SHORT).show();

  }

  private void initView() {
    btnPlay = ((Button) findViewById(R.id.btn_play));
    btnStop = ((Button) findViewById(R.id.btn_stop));
    ivPlayPic = ((ImageView) findViewById(R.id.iv_play_pic));
    ivControl = ((ImageView) findViewById(R.id.iv_control));
  }

  @Override
  public void onClick(View view) {
    switch (view.getId()) {
      case R.id.iv_play_pic:
        int visibility = ivControl.getVisibility();
        if (visibility== View.GONE) {
          ivControl.setVisibility(View.VISIBLE);
        }else if(visibility== View.VISIBLE){
          ivControl.setVisibility(View.GONE);
        }
        break;
      case R.id.btn_play:
        playRadio();
        break;
      case R.id.btn_stop:
        pauseRadio();
        break;
    }
  }



  @Override
  protected void onPause() {
    super.onPause();
//    pauseRadio();
  }

  //播放广播
  private void playRadio() {
    Intent intent = new Intent(PlayService.ACTION_PLAY);
    intent.setPackage(getPackageName());
    intent.putExtra("playUrl",playUrl);
    intent.putExtra("radioName",radioName);
    startService(intent);
  }
  //暂停广播
  private void pauseRadio() {
    Intent intent = new Intent(PlayService.ACTION_PAUSE);
    intent.setPackage(getPackageName());
    startService(intent);
  }
  //停止广播
  private void stopRadio() {
    Intent intent = new Intent(PlayService.ACTION_EXIT);
    intent.setPackage(getPackageName());
    startService(intent);
  }

  @Override
  public void onBackPressed() {
    super.onBackPressed();

  }
}
